package org.alwayssuper.protocol.commons.transform.realtimev2016;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 2016版燃料电池数据格式 (0x03)
 * 参考GB/T 32960.3-2016标准表12
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class FuelCellData {
    @Field(length = 2, desc = "燃料电池电压:0~2000V,精度0.1V,0xFFFF无效")
    private int voltage;
    
    @Field(length = 2, desc = "燃料电池电流:0~2000A,精度0.1A,0xFFFF无效")
    private int current;
    
    @Field(length = 2, desc = "燃料消耗率:0~600kg/100km,精度0.01kg/100km,0xFFFF无效")
    private int fuelConsumptionRate;
    
    // 燃料电池温度探针总数:有效值范围0~65531,0xFFFF无效(由temperatureValues的totalUnit获取)
    private int temperatureProbeCount;
    
    @Field(totalUnit = 2, desc = "探针温度值列表:偏移40℃,精度1℃,有效范围-40℃~+200℃")
    private List<TemperatureProbe> temperatureValues;
    
    @Field(length = 2, desc = "氢系统中最高温度:偏移40℃,精度0.1℃,有效范围-40℃~200℃,0xFFFF无效")
    private int maxTemperatureInHydrogenSystem;
    
    @Field(length = 1, desc = "氢系统中最高温度探针代号:有效范围1~252,0xFF无效")
    private int maxTemperatureProbeNo;
    
    @Field(length = 2, desc = "氢气最高浓度:0~50000mg/kg,精度1mg/kg,0xFFFF无效")
    private int maxHydrogenConcentration;
    
    @Field(length = 1, desc = "氢气最高浓度传感器代号:有效范围1~252,0xFF无效")
    private int maxHydrogenConcentrationSensorNo;
    
    @Field(length = 2, desc = "氢气最高压力:0~100MPa,精度0.1MPa,0xFFFF无效")
    private int maxHydrogenPressure;
    
    @Field(length = 1, desc = "氢气最高压力传感器代号:有效范围1~252,0xFF无效")
    private int maxHydrogenPressureSensorNo;
    
    @Field(length = 1, desc = "高压DC/DC状态:0x01:工作;0x02:断开;0xFE:异常;0xFF:无效")
    private int highVoltageDcDcStatus;
    
    // 温度探针内部类
    @ToString
    @Data
    @Accessors(chain = true)
    public static class TemperatureProbe {
        @Field(length = 1, desc = "探针温度值:偏移40℃,精度1℃,有效范围-40℃~+200℃")
        private int temperature;
        
        /**
         * 获取温度值 (℃)
         * 传输值范围: 0~240, 偏移量40℃, 表示 -40℃~+200℃, 精度 1℃
         */
        public double getTemperature() {
            return temperature - 40.0;
        }
        
        /**
         * 设置温度值 (℃)
         * 值范围: -40℃~+200℃
         */
        public void setTemperature(double temperature) {
            if (temperature < -40.0 || temperature > 200.0) {
                this.temperature = 0xFF; // 无效
            } else {
                this.temperature = (int) Math.round(temperature + 40);
            }
        }
    }
    
    // 通过temperatureValues列表长度返回temperatureProbeCount
    public int getTemperatureProbeCount() {
        return temperatureValues == null ? 0 : temperatureValues.size();
    }
    
    // =========================== 传输值与真实值转换方法 ===========================
    
    /**
     * 获取燃料电池电压值 (V)
     * 传输值范围: 0~20000, 表示 0V~2000V, 精度 0.1V
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getVoltage() {
        if (voltage == 0xFFFE || voltage == 0xFFFF) {
            return Double.NaN;
        }
        return voltage * 0.1;
    }
    
    /**
     * 设置燃料电池电压值 (V)
     * 值范围: 0V~2000V
     */
    public void setVoltage(double voltage) {
        if (Double.isNaN(voltage) || voltage < 0 || voltage > 2000.0) {
            this.voltage = 0xFFFF; // 无效
        } else {
            this.voltage = (int) Math.round(voltage * 10);
        }
    }
    
    /**
     * 获取燃料电池电流值 (A)
     * 传输值范围: 0~20000, 表示 0A~2000A, 精度 0.1A
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getCurrent() {
        if (current == 0xFFFE || current == 0xFFFF) {
            return Double.NaN;
        }
        return current * 0.1;
    }
    
    /**
     * 设置燃料电池电流值 (A)
     * 值范围: 0A~2000A
     */
    public void setCurrent(double current) {
        if (Double.isNaN(current) || current < 0 || current > 2000.0) {
            this.current = 0xFFFF; // 无效
        } else {
            this.current = (int) Math.round(current * 10);
        }
    }
    
    /**
     * 获取燃料消耗率值 (kg/100km)
     * 传输值范围: 0~60000, 表示 0~600kg/100km, 精度 0.01kg/100km
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getFuelConsumptionRate() {
        if (fuelConsumptionRate == 0xFFFE || fuelConsumptionRate == 0xFFFF) {
            return Double.NaN;
        }
        return fuelConsumptionRate * 0.01;
    }
    
    /**
     * 设置燃料消耗率值 (kg/100km)
     * 值范围: 0~600kg/100km
     */
    public void setFuelConsumptionRate(double fuelConsumptionRate) {
        if (Double.isNaN(fuelConsumptionRate) || fuelConsumptionRate < 0 || fuelConsumptionRate > 600.0) {
            this.fuelConsumptionRate = 0xFFFF; // 无效
        } else {
            this.fuelConsumptionRate = (int) Math.round(fuelConsumptionRate * 100);
        }
    }
    
    /**
     * 获取氢系统中最高温度值 (℃)
     * 传输值范围: 0~2400, 偏移量40℃, 表示 -40℃~200℃, 精度 0.1℃
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getMaxTemperatureInHydrogenSystem() {
        if (maxTemperatureInHydrogenSystem == 0xFFFE || maxTemperatureInHydrogenSystem == 0xFFFF) {
            return Double.NaN;
        }
        return maxTemperatureInHydrogenSystem * 0.1 - 40.0;
    }
    
    /**
     * 设置氢系统中最高温度值 (℃)
     * 值范围: -40℃~200℃
     */
    public void setMaxTemperatureInHydrogenSystem(double maxTemperatureInHydrogenSystem) {
        if (Double.isNaN(maxTemperatureInHydrogenSystem) || maxTemperatureInHydrogenSystem < -40.0 || maxTemperatureInHydrogenSystem > 200.0) {
            this.maxTemperatureInHydrogenSystem = 0xFFFF; // 无效
        } else {
            this.maxTemperatureInHydrogenSystem = (int) Math.round((maxTemperatureInHydrogenSystem + 40.0) * 10);
        }
    }
    
    /**
     * 获取氢气最高浓度值 (mg/kg)
     * 传输值范围: 0~60000, 表示 0~50000mg/kg, 精度 1mg/kg
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getMaxHydrogenConcentration() {
        if (maxHydrogenConcentration == 0xFFFE || maxHydrogenConcentration == 0xFFFF) {
            return Double.NaN;
        }
        return maxHydrogenConcentration;
    }
    
    /**
     * 设置氢气最高浓度值 (mg/kg)
     * 值范围: 0~50000mg/kg
     */
    public void setMaxHydrogenConcentration(double maxHydrogenConcentration) {
        if (Double.isNaN(maxHydrogenConcentration) || maxHydrogenConcentration < 0 || maxHydrogenConcentration > 50000) {
            this.maxHydrogenConcentration = 0xFFFF; // 无效
        } else {
            this.maxHydrogenConcentration = (int) Math.round(maxHydrogenConcentration);
        }
    }
    
    /**
     * 获取氢气最高压力值 (MPa)
     * 传输值范围: 0~1000, 表示 0MPa~100MPa, 精度 0.1MPa
     * 0xFFFE: 异常, 0xFFFF: 无效
     */
    public double getMaxHydrogenPressure() {
        if (maxHydrogenPressure == 0xFFFE || maxHydrogenPressure == 0xFFFF) {
            return Double.NaN;
        }
        return maxHydrogenPressure * 0.1;
    }
    
    /**
     * 设置氢气最高压力值 (MPa)
     * 值范围: 0MPa~100MPa
     */
    public void setMaxHydrogenPressure(double maxHydrogenPressure) {
        if (Double.isNaN(maxHydrogenPressure) || maxHydrogenPressure < 0 || maxHydrogenPressure > 100.0) {
            this.maxHydrogenPressure = 0xFFFF; // 无效
        } else {
            this.maxHydrogenPressure = (int) Math.round(maxHydrogenPressure * 10);
        }
    }
}